package com.devframe.web.taglib;

import java.util.Enumeration;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

/**
 * 
 * <pre>
 * &lt;b&gt;&lt;font color=&quot;blue&quot;&gt;SelectTag&lt;/font&gt;&lt;/b&gt;
 * </pre>
 * 
 * <pre>
 * &lt;b&gt; --页面路径及请求参数输出标签--&lt;/b&gt;
 * 可以控制哪些参数无需输出
 * 可以控制是否输入页面路径
 * </pre>
 * <pre>
 * </pre>
 * <pre>
 * &lt;b&gt;--样例--&lt;/b&gt;
 * </pre>
 * 
 * JDK版本：JDK1.4
 * 
 * 
 */
public class PageUrlTag extends AbstractBaseTag {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 7993237667875479682L;
    
    /**
     * excludeParams
     * 排除参数列表，用逗号或者分号或者空格分隔。
     */
    private String excludeParams;
    /**
     * includePath
     * 是否包含页面路径：true/false。默认true
     */
    private String includePath="true";
    /**
     * includeParam
     * 是否包含请求参数：true/false。默认true
     */
    private String includeParam="true";
    /**
     * 标签处理逻辑
     * 
     * @return SKIP_BODY
     */
    public int doStartTag() {

        return SKIP_BODY;
    }

    /**
     * doEndTag
     * 
     * @return EVAL_PAGE
     * @throws JspException
     *             JspException
     */
    public int doEndTag() throws JspException {
        try {
            HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
            
            StringBuffer pageUrl = new StringBuffer();
            if(includeParam!=null && includeParam.equalsIgnoreCase("true")){
                Set excludeParamSet = new HashSet();
                if(excludeParams!=null){
                    String[] excludeParamArr = excludeParams.split("[,; ]");
                    for(int i=0;i<excludeParamArr.length;i++){
                        excludeParamSet.add(excludeParamArr[i]);
                    }
                }
                Enumeration enu = request.getParameterNames();
                while (enu.hasMoreElements()) {
                    String paramName = (String) enu.nextElement();
                    //在排除参数列表中
                    if(excludeParamSet.contains(paramName)){
                        continue;
                    }
                    String[] paramValues = request.getParameterValues(paramName);
                    if (paramValues != null) {
                        for (int i = 0; i < paramValues.length; i++) {
                            if (pageUrl.length() > 0) {
                                pageUrl.append("&");
                            }
                            pageUrl.append(paramName).append("=").append(paramValues[i]);
                        }
                    }
                }
            }
            if(includePath!=null && includePath.equalsIgnoreCase("true")){
                if(includeParam!=null && includeParam.equalsIgnoreCase("true")){
                    pageUrl.insert(0, "?");
                }
                pageUrl.insert(0, request.getRequestURI());
            }
            JspWriter writer = null;
            try {
                writer = this.getPreviousOut();
            } catch (Exception e1) {
                writer = pageContext.getOut();
            }

            try {
                writer.print(pageUrl);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return EVAL_PAGE;
        } catch (Exception e) {
            return EVAL_PAGE;
        }
    }

    /**
     * release
     * 
     * @see javax.servlet.jsp.tagext.BodyTagSupport#release()
     */
    public void release() {
        super.release();
    }

    public String getExcludeParams() {
        return excludeParams;
    }

    public void setExcludeParams(String excludeParams) {
        this.excludeParams = excludeParams;
    }

    public String getIncludePath() {
        return includePath;
    }

    public void setIncludePath(String includePath) {
        this.includePath = includePath;
    }

    public String getIncludeParam() {
        return includeParam;
    }

    public void setIncludeParam(String includeParam) {
        this.includeParam = includeParam;
    }
    
    public static void main(String[] args){
        String ps="a,b,c;d,e f,aa，bbb，爱爱爱";
        String[] arr = ps.split("[,; ，]");
        for(int i=0;i<arr.length;i++){
            System.out.println(arr[i]);            
        }
    }

}
